package com.rui.study.commons.study.web;

import com.rui.study.commons.model.BizException;
import com.rui.study.commons.model.Result;
import com.rui.study.commons.model.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;


public class BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseController.class);


    @InitBinder
    public void hello(WebDataBinder binder) {
        //...
    }

    @ExceptionHandler
    public Result handleBizException(BizException biz) {
        String status = biz.getStatus();
        String message = biz.getMessage();
        LOGGER.info("'BizException >.>.> {} :{}", status, message);
        return new Result(status, message);
    }


    @ExceptionHandler
    public Result handleException(Exception e) {
        StatusCode statusCode = StatusCode.SYSTEM_EXCEPTION;
        StringBuffer msg = new StringBuffer(statusCode.getMessage()).append(": ");
        msg.append(e.getMessage());
        LOGGER.info("Catched an 'Exception', caused by {}", e.getMessage());
        return new Result(statusCode.getStatus(), "请联系管理员");
    }

}
